import api
import model
import os
import subprocess
import sys

wiki_dir = sys.path[0] + '/server.wiki/'

# Clone or pull.
if os.path.exists(wiki_dir):
    p = subprocess.Popen('git pull', cwd=wiki_dir, shell=True)
else:
    sh = 'git clone git@github.com:lpy/server.wiki.git'
    p = subprocess.Popen(sh, cwd=sys.path[0], shell=True)
p.wait()


def make_api_page(filename, func_list):
    with open(wiki_dir + '.'.join([filename, 'markdown']), 'w') as f:
        for func in func_list:
            f.write(func.func_doc)


def make_model_page(filename, model):
    with open(wiki_dir + '.'.join([filename, 'markdown']), 'w') as f:
        f.write(model.__doc__)

wiki_api = {
    'API-User': [
        api.user.new_user,
        api.user.user_info,
        api.user.user_login,
        api.user.update_user_info,
        api.user.update_user_password,
        api.user.get_user_problems,
        api.user.new_user_problem,
        api.user.new_user_comment,
        api.user.get_user_comments,
        api.user.new_user_answer,
        api.user.get_user_ask_me,
        api.user.weibo_oauth_callback,
        api.user.get_user_follow,
        api.user.update_user_follow,
        api.user.get_user_fan,
        api.user.update_user_studio,
        api.user.find_user_by_name,
        api.user.update_user_collection,
        api.user.get_user_collection,
        api.user.get_user_subscription,
        api.user.get_user_relation,
        api.user.get_user_work,
        api.user.update_user_work,
        api.user.new_user_studio_comment,
        api.user.get_user_answer
    ],
    'API-Answer': [
        api.answer.update_answer
    ],
    'API-Comment': [
    ],
    'API-Problem': [
        api.problem.get_problem,
        api.problem.update_zan,
        api.problem.get_problem_comments,
        api.problem.get_problem_answers,
        api.problem.get_problem_list
    ],
    'API-Classification': [
        api.classification.new_classification,
        api.classification.get_classification,
        api.classification.upload_image,
        api.classification.get_index,
        api.classification.get_all_subclass,
        api.classification.get_list,
        api.classification.update_classification,
        api.classification.remove
    ],
    'API-Image': [
        api.image.get_image_upload_url,
        api.image.upload_image_url
    ],
    'API-Video': [
        api.video.delete_video,
        api.video.get_video_info,
        api.video.get_video_upload_url,
        api.video.new_video,
        api.video.update_video_info,
        api.video.upload_video
    ],
    'API-Article': [
        api.article.get_all_article,
        api.article.get_article
    ],
    'API-Studio': [
        api.studio.get_all_studio,
        api.studio.new_studio,
        api.studio.studio_login,
        api.studio.get_studio,
        api.studio.get_studio_article,
        api.studio.get_studio_fan,
        api.studio.get_studio_follow,
        api.studio.get_studio_teacher,
        api.studio.get_studio_video,
        api.studio.get_studio_work,
        api.studio.get_studio_recruiment,
        api.studio.update_studio_teacher,
        api.studio.update_studio_video,
        api.studio.update_studio_work,
        api.studio.delete_studio_work,
        api.studio.update_studio_info,
        api.studio.update_studio_recruitment,
        api.studio.new_article,
        api.studio.new_recruitment,
        api.studio.get_studio_relation,
        api.studio.get_studio_ask_me,
        api.studio.new_studio_answer,
        api.studio.update_studio_answer,
        api.studio.get_studio_answer,
        api.studio.get_studio_studio_comment,
        api.studio.reply_studio_comment,
        api.studio.delete_studio_comment,
        api.studio.update_studio_environment,
        api.studio.delete_studio_environment,
        api.studio.get_studio_environment,
        api.studio.update_studio_avatar
    ],
    'API-Merchant': [
        api.merchant.get_all_merchants,
        api.merchant.get_merchant,
        api.merchant.get_merchant_goods
    ],
    'API-Good': [
        api.good.get_good
    ]
}
wiki_model = {
    'Model-Answer': model.answer.Answer,
    'Model-Comment': model.comment.Comment,
    'Model-Problem': model.problem.Problem,
    'Model-User': model.user.User,
    'Model-Classification': model.classification.Classification,
    'Model-Video': model.video.Video,
    'Model-Article': model.article.Article,
    'Model-Studio': model.studio.Studio,
    'Model-Merchant': model.merchant.Merchant,
    'Model-Good': model.good.Good,
    'Model-StudioComment': model.studio_comment.StudioComment
}

# Generate wiki pages for apis.
for page_name, func_list in wiki_api.items():
    make_api_page(page_name, func_list)

# Generate wiki pages for models.
for page_name, model_class in wiki_model.items():
    make_model_page(page_name, model_class)

# Commit and push.
sh = 'git add --all && git commit -m "Update Wiki" && git push'
p = subprocess.Popen(sh, cwd=wiki_dir, shell=True)
p.wait()

